//
//  LogInViewController.m
//  E_ChatRoom
//
//  Created by rimi1 on 15/6/27.
//  Copyright (c) 2015年 TJ. All rights reserved.
//

#import "LogInViewController.h"
#import "OtherLogInViewController.h"
#import "RegisterViewController.h"
#import "ECRNavigationController.h"

@interface LogInViewController ()<UITextFieldDelegate,RegisgerViewControllerDelegate>

/*头像*/
@property (nonatomic, strong)UIImageView *headPicView;
/*用户名*/
@property (nonatomic, strong)UILabel *userName;
/*密码输入框*/
@property (nonatomic, strong)UITextField *pwdFiled;
/*登陆*/
@property (nonatomic, strong)UIButton *loginBtn;
/*注册*/
@property (nonatomic, strong)UIButton *registBtn;
/*忘记密码*/
@property (nonatomic, strong)UIButton *forgrtPsdBtn;
/*其他方式登陆*/
@property (nonatomic, strong)UIButton *otherLoginBtn;

@end

@implementation LogInViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"登陆";
        self.view.backgroundColor = [UIColor lightGrayColor];
    }
    return self;
}

#pragma mark - life circle methods

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initializeUsreinterface];
}


#pragma mark - textFiled Delegate method

/**
 *  退出键盘
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}


#pragma mark - private methods（私有方法）

/**
 *  初始化用户界面
 */
- (void)initializeUsreinterface{
    
    //0.头像
    _headPicView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100*Ratio, 100*Ratio)];
    _headPicView.center = CGPointMake(self.view.center.x, self.view.center.y - 200*Ratio);
    _headPicView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_headPicView];
    
    //1.用户名Label
    _userName = [[UILabel alloc] init];
    _userName.frame = CGRectMake(CGRectGetMinX(_headPicView.frame), CGRectGetMaxY(_headPicView.frame), 100*Ratio, 25);
    _userName.backgroundColor = [UIColor yellowColor];
    //从沙盒获取用户名
    NSString *user = [UserInfo sharedUserInfo].user;
    self.userName.text = user;
    [self.view addSubview:_userName];
    
    //2.密码输入框
    CGFloat pwdFiledX = 20*Ratio;
    CGFloat pwdFiledY = CGRectGetMaxY(_userName.frame) + 20*Ratio;
    CGFloat pwdFiledW = (ScreenWidth - pwdFiledX*2);
    CGFloat pwdFiledH = 28*Ratio;
    _pwdFiled = [[UITextField alloc] initWithFrame:CGRectMake(pwdFiledX, pwdFiledY, pwdFiledW, pwdFiledH)];
    _pwdFiled.delegate = self;
    _pwdFiled.backgroundColor = [UIColor whiteColor];
    [_pwdFiled addLeftViewWithImage:@"Card_Lock"];
    [self.view addSubview:_pwdFiled];
    
    //3.登陆
    CGFloat loginBtnX = pwdFiledX;
    CGFloat loginBtnY = CGRectGetMaxY(_pwdFiled.frame) + 20*Ratio;
    CGFloat loginBtnW = (ScreenWidth - loginBtnX*2);
    CGFloat loginBtnH = 30*Ratio;
    _loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(loginBtnX, loginBtnY, loginBtnW, loginBtnH)];
    _registBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
    [_loginBtn setResizeN_BG:@"fts_green_btn" H_BG:@"fts_green_btn_HL"];
    [_loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginBtn];
    
    //4.注册
    CGFloat registBtnW = 50*Ratio;
    CGFloat registBtnH = 20*Ratio;
    CGFloat registBtnX = (CGRectGetWidth(_loginBtn.frame)*0.5 - registBtnW)*0.5;
    CGFloat registBtnY = CGRectGetMaxY(_loginBtn.frame) + 15*Ratio;
    _registBtn = [[UIButton alloc] initWithFrame:CGRectMake(registBtnX, registBtnY, registBtnW, registBtnH)];
    [_registBtn setTitle:@"注册" forState:UIControlStateNormal];
    _registBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _registBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_registBtn addTarget:self action:@selector(regist) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registBtn];
    
    //5.忘记密码
    CGFloat forgrtPsdBtnW = 150*Ratio;
    CGFloat forgrtPsdBtnH = 20*Ratio;
    CGFloat forgrtPsdBtnX = CGRectGetMaxX(_registBtn.frame) + 40*Ratio;
    CGFloat forgrtPsdBtnY = CGRectGetMaxY(_loginBtn.frame) + 15*Ratio;
    _forgrtPsdBtn = [[UIButton alloc] initWithFrame:CGRectMake(forgrtPsdBtnX, forgrtPsdBtnY, forgrtPsdBtnW, forgrtPsdBtnH)];
    _forgrtPsdBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _forgrtPsdBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_forgrtPsdBtn setTitle:@"忘记密码怎么办?" forState:UIControlStateNormal];
    [_forgrtPsdBtn addTarget:self action:@selector(forgrtPsw) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_forgrtPsdBtn];
    
    //6.其他方式登陆
    _otherLoginBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    _otherLoginBtn.center = CGPointMake( self.view.center.x, CGRectGetHeight(self.view.frame) - 100*Ratio);
    [_otherLoginBtn setTitle:@"其他方式登陆" forState:UIControlStateNormal];
    _otherLoginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _otherLoginBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_otherLoginBtn addTarget:self action:@selector(otherBtnLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_otherLoginBtn];
}

/**
 *  用户登录
 */

- (void)login{
    
    //1.保存数据到沙盒
    UserInfo *userInfo = [UserInfo sharedUserInfo];
    userInfo.user = self.userName.text;
    userInfo.pwd = self.pwdFiled.text;
    
    //2.执行登陆
    [super login];
}
/**
 *  用户注册
 */
- (void)regist{
    
    //1.用导航控制器包装注册控制器
    RegisterViewController *regist = [[RegisterViewController alloc] init];
    regist.delegate = self;
    ECRNavigationController *nav = [[ECRNavigationController alloc] initWithRootViewController:regist];
    
    //2.模态推送
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark regisgerViewController的代理
-(void)regisgerViewControllerDidFinishRegister{
    
    // 完成注册 userLabel显示注册的用户名
    self.userName.text = [UserInfo sharedUserInfo].registerUser;
    // 提示
    [MBProgressHUD showSuccess:@"请重新输入密码进行登录" toView:self.view];
}

/**
 *  其他方式登陆
 */
- (void)otherBtnLogin{
    
    //1.用导航控制器包装注册控制器
    ECRNavigationController *nav = [[ECRNavigationController alloc] initWithRootViewController:[[OtherLogInViewController alloc] init]];
    
    //2.模态推送
    [self presentViewController:nav animated:YES completion:nil];
}

/**
 *  忘记密码
 */
- (void)forgrtPsw{
    
    [MBProgressHUD showSuccess:@"该功能正在维护中"];
}

/**
 *  点击空白处，退出键盘
 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}
@end
